//
//  WebVC.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/9.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import WebKit

class WebVC: UIViewController {

    var webview:WKWebView!
    
    var url:String
    
    
    init(urlstr:String) {
        
        url = urlstr
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        setupData()
    }
    
    private func setupUI() {
        
        setupBackItem()
        
        webview = WKWebView(frame: CGRect(x: 0, y: kNavigationBarHeight, width: kScreenWidth, height: kScreenHeight - kNavigationBarHeight), configuration: WKWebViewConfiguration())
        webview.uiDelegate = self
        webview.navigationDelegate = self
        
        self.view.addSubview(webview)
        
        self.closeAutomaticallyInsets(scrollview: webview.scrollView)
    }
    
    private func setupData() {
        
        if let url = URL(string: self.url) {
            
            webview.load(URLRequest(url: url))
        } else {
            
            self.view.makeToast(LocalizedString("network_error"))
        }
        
    }
}

extension WebVC: WKUIDelegate, WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        
        webView.evaluateJavaScript("document.documentElement.style.webkitUserSelect='none'", completionHandler: nil)
        webView.evaluateJavaScript("document.documentElement.style.webkitTouchCallout='none'", completionHandler: nil)
    }
}
